package org.despair2D.ui.supportClasses 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import org.despair2D.debug.Logger;
	import org.despair2D.resource.ILoader;
	import org.despair2D.resource.LoaderManager;
	import org.despair2D.ui.ButtonType;
	import org.despair2D.ui.puppet.ImagePuppet;

	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	
public class AbstractImageButton extends DisplayObjectContainerButton 
{
	
	/**
	 * 加入画像数据
	 * @param	source
	 * @param	dataName
	 * @param	type
	 */
	public static function addImageButtonData( source:*, dataName:String, buttonType:int ) : void
	{		
		var type:Class
		var BP:Bitmap
		var BA:BitmapData
		var prop:ImageButtonProp
		
		if (m_imageButtonPropList[dataName])
		{
			//Logger.reportWarning('AbstractImageButton', 'addImageButtonData', '存在的数据: ' + dataName)
			return
		}
		
		if (source is BitmapData)
		{
			BA = source as BitmapData
		}
		
		else if (source is Class)
		{
			type = source as Class
			
			try
			{
				BP = new type()
			}
			
			catch (error:Error)
			{
				Logger.reportError('addImageButtonData', 'addImageButtonData', 'source类型错误.')
			}
			
			BA = BP.bitmapData
		}
		
		else
		{
			Logger.reportError('AbstractImageButton', 'constructor', '参数错误.')
		}
		
		switch(buttonType & 0x00F)
		{
			case ButtonType.BUTTON:
				prop = new ImageButtonProp();
				prop.initialize(BA, false)
				break;
				
			case ButtonType.BUTTON_HAS_INVALID:
				prop = new ImageButtonProp();
				prop.initialize(BA, true)
				break;
				
			case ButtonType.CHECKBOX:
				prop = new ImageCheckBoxProp();
				prop.initialize(BA, false)
				break;
				
			case ButtonType.CHECKBOX_HAS_INVALID:
				prop = new ImageCheckBoxProp();
				prop.initialize(BA, true)
				break;
		}
		
		prop.effectType = buttonType & 0x0F0
		m_imageButtonPropList[dataName] = prop
	}
	
	
	public function AbstractImageButton( dataName:String, isCenter:Boolean )
	{
		super();
		
		m_isCenter  =  isCenter
		m_prop      =  m_imageButtonPropList[dataName]
		if (!m_prop)
		{
			Logger.reportError(this, 'constructor', '不存在的画像按钮数据.')
		}
		
		this.parseSource()
		this.adjustAlign()
	}
	
	
	final ns_despair function adjustAlign() : void
	{	
		if (m_isCenter)
		{
			m_bitmap.x = -m_bitmap.width  * 0.5
			m_bitmap.y = -m_bitmap.height * 0.5
		}
	}
	
	final override ns_despair function dispose() : void
	{
		if (m_bitmap)
		{
			m_bitmap.bitmapData = null
			m_bitmap = null
		}
		
		super.dispose();
	}
	
	final ns_despair function parseSource() : void
	{
		m_bitmap = new Bitmap()
		m_container.addChild(m_bitmap)
		m_releaseListener.execute()
		
		if (m_prop.hasInvalid)
		{
			this.addInvalidEffectListener(onInvalid)
		}
	}
	
	
	ns_despair static var m_imageButtonPropList:Object = new Object()
	
	ns_despair var m_bitmap:Bitmap
	
	ns_despair var m_prop:ImageButtonProp
	
	ns_despair var m_isCenter:Boolean
}
}